<?php

define('CURSCRIPT', 'system');
require_once './core/init.inc.php';
$navtitle.= ' - 系统配置';

if(!$_G['admin']->isLogged() || !$_G['admin']->isSuperAdmin()){
	showmsg('非法操作。您不是本系统的所有者（超级管理员）。', 'back');
}

switch($_GET['action']){
case 'listadmin':
	$limit = 12;
	$offset = ($page - 1) * $limit;
	$adminlist = Admin::getAdminList($offset, $limit);
	$admintotal = Admin::getAdminNum();

	include view('admin_list');
break;

case 'editadmin':
	if(!$_POST){
		$uid = max(0, intval($_GET['uid']));
		$a = new Admin($uid);
		if($uid == 0){
			$a->v['permission'] = 0x1;
		}

		include view('admin_edit');
	}else{
		$a = array(
			'uid' => intval($_POST['a']['uid']),
			'username' => $_POST['a']['username'],
		);
		if($_POST['a']['pw']){
			if($_POST['a']['pw'] != $_POST['a']['pw2']){
				showmsg('两次输入的密码不同，请重新输入。', 'back');
			}
			$a['password'] = $_POST['a']['pw'];
		}
		if(empty($a['username'])){
			showmsg('请输入管理员的账号。', 'back');
		}
		$p = $_POST['p'];
		$admin = new Admin($a['uid']);
		$admin->v['username'] = $a['username'];
		$admin->v['password'] = $a['password'];
		$admin->setPermission($p);
		
		if($a['uid']){
			showmsg('成功修改管理员信息！', 'system.php?action=listadmin');
		}else{
			if($admin->register() > 0){
				showmsg('成功添加新管理员！', 'system.php?action=listadmin');
			}else{
				showmsg('添加管理员失败。', 'system.php?action=listadmin');
			}
		}
	}
break;

case 'deleteadmin':
	if($uid = intval($_GET['uid'])){
		if(!$_GET['confirm']){
			showmsg('您确认删除该管理员吗？', 'confirm');
		}
		$affected_rows = Admin::deleteAdmin($uid);
		if($affected_rows > 0){
			showmsg('删除成功！', 'system.php?action=listadmin');
		}else{
			showmsg('删除失败，该管理员不存在或该管理员是超级管理员。', 'system.php?action=listadmin');
		}
	}else{
		showmsg('非法操作，管理员ID错误。');
	}
break;

case 'type':
	$db->select_table('projecttype');
	if($_POST){
		if(is_array($_POST['name'])){
			$data = array();
			foreach($_POST['name'] as $t){
				if(!empty($t)){
					$data[] = array('name' => $t);
				}
			}
			if(!$data){
				showmsg('您没有添加任何新项目类型。', 'back');
			}
	
			$db->INSERTS($data);
			updatecache('project_type');
			showmsg('成功添加新项目类型！', 'refresh');

		}else if($typeid = intval($_POST['typeid'])){
			if(isset($_POST['name'])){
				$db->UPDATE(array('name' => $_POST['name']), 'id='.$typeid);
				if($db->affected_rows() == 1){
					updatecache('project_type');
					showmsg('成功修改项目类型！', 'refresh');
				}else{
					showmsg('该项目类型不存在或已被删除。请添加新类型。', 'back');
				}
			}else{
				if(!$_GET['confirm']){
					showmsg('您确认删除该类型吗？', 'confirm');
				}else{
					$db->DELETE('id='.$typeid);
					if($db->affected_rows() == 1){
						updatecache('project_type');
						showmsg('该项目类型成功删除！', 'refresh');
					}else{
						showmsg('该项目类型不存在或已被删除。', 'back');
					}
				}
			}
		}
	}else{
		$types = $db->MFETCH('*');
		include view('system_projecttype');
	}
break;

case 'config':default:
	$styles = array();
	$styledir = opendir(S_ROOT.'image/');
	while($dir = readdir($styledir)){
		if(is_dir(S_ROOT.'image/'.$dir) && file_exists(S_ROOT.'image/'.$dir.'/common.css')){
			$styles[$dir] = $dir;
		}
	}
	closedir($styledir);

	if($_POST){
		$setting = array(
			'home_unusedratio' => intval($_POST['home_unusedratio']),
			'home_deadlinelimit' => intval($_POST['home_deadlinelimit']),
			'timezone' => intval($_POST['timezone']),
			'timefix' => intval($_POST['timefix']),
			'cookiepre' => $_POST['cookiepre'],
			'charset' => $_POST['charset'],
			'style' => $_POST['style'],
		);
		
		if(!isset($styles[$setting['style']])){
			$setting['style'] = 'default';
		}

		file_put_contents(S_ROOT.'./data/config.inc.php', '<?php return '.var_export($setting, true).';?>');
		showmsg('成功修改系统配置！', 'refresh');
	}

	include view('system_config');
}

?>